package com.cxc.singleton;


import java.io.ObjectStreamException;
import java.io.Serializable;

/**
 * author cxc
 *
 */
public class SingletonTest implements Serializable {

	private static SingletonTest instance;  
	
	private SingletonTest(){
		if(instance!=null){
			System.out.println();
//			throw new RuntimeException();
		}
	}
	
	public static  synchronized SingletonTest  getInstance(){
		if(instance==null){
			instance = new SingletonTest();
		}
		return instance;
	}
	
	//反序列化时，如果定义了readResolve()则直接返回此方法指定的对象。而不需要单独再创建新对象！
	private Object readResolve() throws ObjectStreamException {
		return instance;
	}
}
